Tuesday, July 7, 2020

ATMega32 SPI Interfaces To SN74HC164 And LED

ATMega32 contains an SPI communication module. This type of communication interface typically uses 3 wires plus chip select pins. However this controller uses only two pins - serial data and serial clock pin to interface with SN74HC164 parallel out shift register chip.

ATMega32 SPI Interfaces To SN74HC164 And LED
Simulating Program

ATMega32 SPI Interfaces To SN74HC164 And LED
SN74HC164 DIP-14

I use Hardware SPI with the clock rate Fosc/4. Microcontroller clock is internal RC running at 4MHz.

  1. /*
  2.  * SPI74164LED.c
  3.  *
  4.  * Created: 7/7/2023 9:16:53 AM
  5.  * Author : Admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. #define F_CPU 4000000UL
  11. #include <util/delay.h>
  12.  
  13. void masterInit(void){
  14. /*Set MOSI, SCK and SS Output*/
  15. DDRB|=(1<<4)|(1<<5)|(1<<7);
  16. /*Enable SPI Master Set Clock Rate Fclk/4*/
  17. SPCR|=(1<<SPE)|(1<<MSTR);
  18. }
  19.  
  20. void masterTransmit(char spiData){
  21. /*Start The Transmission*/
  22. SPDR = spiData;
  23. /*Wait For Completion*/
  24. while(!(SPSR&(1<<SPIF)));
  25. }
  26.  
  27.  
  28. int main(void)
  29. {
  30. masterInit();
  31.  
  32. while (1)
  33. {
  34. masterTransmit(0xF0);
  35. _delay_ms(1000);
  36. masterTransmit(0xAA);
  37. _delay_ms(1000);
  38. masterTransmit(0x0F);
  39. _delay_ms(1000);
  40. }
  41. }
  42.  
  43.  

Click here to download its source file.

No comments:

Post a Comment